<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Config;
use think\Request;

class Message extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 首页
     * @return mixed
     */
    public function index(){
        if($this::$isdelete){
           $map['m.isdelete']=1;
        }else{
            $map['m.isdelete']=0;
        }
        $data=input();
        if($data['realname']){
            $map['m.realname']=array('like',"%{$data['realname']}%");
        }
        if($data['content']){
            $map['m.content']=array('like',"%{$data['content']}%");
        }
        $listRows = $this->request->param('numPerPage') ? : Config::get("paginate.list_rows");
        $list = Db::name('message')
            ->alias('m')
            ->join('admin_user u','u.id=m.user_id')
            ->field('m.*,u.realname')
            ->where($map)
            ->order("id desc")
            ->paginate($listRows, false, ['query' => $this->request->get()]);
        $this->view->assign('list',$list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());
        return $this->view->fetch();
    }

    /**
     * 编辑
     * @return mixed
     */
    public function edit(){
        $data  = input();
        $id = $data['id'];
        if($this->request->isAjax()){
            unset($data['id']);
            $rs = DB::name('message')->where('id='.$id)->update($data);
            if($rs!=false){
                return ajax_return_adv("修改成功");
            }else{
                return ajax_return_error("修改失败");
            }
        }else{

            $user=DB::name('message')
                ->alias('m')
                ->join('admin_user u','m.user_id=u.id')
                ->where('m.id='.$id)
                ->find();
            $this->view->assign('vo', $user);
            $users=Db::name("admin_user")->select();
            $this->view->assign('users', $users);
            $this->view->assign('id', $user['id']);
            return $this->view->fetch();
        }
    }

    /**
     * 添加意见反馈
     */
    public function add(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            $insert = Db::name('message')->strict(false)->insertGetId($postData);
            if ($insert){
                return ajax_return_adv('添加成功');
            }else{
                return ajax_return_adv_error('添加失败');
            }
        }else{
            $users = Db::name('admin_user')->select();
            $this->view->assign('users',$users);
            return $this->view->fetch('edit');
        }
    }

    /**
     * 客服信息
     */
    public function service_info(){
        $data = input();
        $map['id'] = array('like',"%{$data['id']}%");
        $map['phone'] = array('like',"%{$data['phone']}%");
        $list = Db::name('service_info')->where($map)->select();
        $this->view->assign('list',$list);
        return $this->view->fetch();
    }

    /**
     * 添加客服信息
     */
    public function add_service_info(Request $request){

        if($request->isAjax()){
            $phone = input('phone');

            if(empty($phone)){
                return ajax_return_adv_error('客服电话不能为空');
            }

            if($phone && !empty($phone)){
                $ck = Db::name('service_info')->where('phone',$phone)->find();
                if($ck){
                    return ajax_return_adv_error('该客服电话已存在');
                }

                $inser = [
                    'phone' => $phone,
                    'create_time' => time()
                ];

                $res = Db::name('service_info')->strict(false)->insert($inser);

                if($res){
                    return ajax_return_adv('添加成功');
                }else{
                    return ajax_return_adv_error('添加失败');
                }
            }
        }else{
            return $this->view->fetch();
        }
    }

    /**
     * 编辑客服信息
     */
    public function edit_service_info(Request $request){
        if($request->isAjax()){
            $data = input();

            if(empty($data['id'])){
                return ajax_return_adv_error('该记录参数有误');
            }

            if(empty($data['phone'])){
                return ajax_return_adv_error('客服电话不能为空');
            }

            $res = Db::name('service_info')->strict(false)->where('id',$data['id'])->update(array('phone'=>$data['phone']));

            if($res){
                return ajax_return_adv('修改成功');
            }else{
                return ajax_return_adv_error('修改失败');
            }
        }else{
            $id = input('id');
            $data = Db::name('service_info')->where('id',$id)->find();
            $this->view->assign('data',$data);
            return $this->view->fetch();
        }
    }

    /**
     * 删除客服信息
     */
    public function del_service_info(){
        $id = input('id');
        if(empty($id)){
            return ajax_return_adv_error('该记录参数有误');
        }
        $res = Db::name('service_info')->where('id',$id)->delete();
        if($res){
            return ajax_return_adv('删除成功');
        }else{
            return ajax_return_adv_error('删除失败');
        }
    }

}


